package com.youlu.campus.website.service.impl;

import com.youlu.campus.website.entity.Partner;
import com.youlu.campus.website.entity.PartnerPageQuery;
import com.youlu.campus.website.repositories.PartnerRepository;
import com.youlu.campus.website.service.PartnerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


/**
 * 合作伙伴业务逻辑层实现类
 *
 * @author su
 * @date 2025/08/25
 */
@Slf4j
@Service
public class PartnerServiceImpl implements PartnerService {

    @Autowired
    private PartnerRepository partnerRepository;

    @Resource(name = "websiteMongoTemplate")
    private MongoTemplate mongoTemplate;

    @Override
    public Partner save(Partner partner) {
        if (StringUtils.isBlank(partner.getId())) {
            partner.setCreateTime(LocalDateTime.now());
            partner.setId(null);
        } else {
            partner.setUpdateTime(LocalDateTime.now());
        }
        return partnerRepository.save(partner);
    }

    @Override
    public Boolean delete(String id) {
        Assert.hasText(id, "合作伙伴 ID 不能为空");
        partnerRepository.deleteById(id);
        return true;
    }

    @Override
    public Partner findById(String id) {
        return partnerRepository.findById(id).orElse(null);
    }

    @Override
    public Page<Partner> findByPage(PartnerPageQuery pageQuery) {
        Query query = new Query();
        if (StringUtils.isNotBlank(pageQuery.getName())) {
            query.addCriteria(Criteria.where("name").regex(pageQuery.getName(), "i"));
        }
        //统计总条数（分页需要先查总数）
        long total = mongoTemplate.count(query, Partner.class);

        Pageable pageRequest = PageRequest.of(pageQuery.getPage() - 1, pageQuery.getPageSize(), Sort.by("createTime").descending());
        List<Partner> list;
        if (total == 0) {
            list = Arrays.asList();
        } else {
            // 应用分页（设置跳过的条数和查询的条数）
            query.skip(pageRequest.getOffset()).limit(pageRequest.getPageSize());
            // 应用排序
            query.with(pageRequest.getSort());
            list = mongoTemplate.find(query, Partner.class);
        }
        return PageableExecutionUtils.getPage(list, pageRequest, () -> total);
    }

    @Override
    public List<Partner> findAll() {
        return partnerRepository.findAll();
    }
}